package com.alipay.app.groupmeal.standard.widget;

import android.content.Context;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.util.Log;
import android.view.KeyEvent;

import com.alipay.app.groupmeal.standard.constant.BaseConstant;
import com.alipay.iot.bpaas.api.utils.StringUtil;
import com.alipay.sdk.util.LogUtil;

import java.math.BigDecimal;

/**
 * author: lixiaoyan
 * date: 12/13/21
 * 自定义自适应文字内容控件带数字输入
 */
public class InputAmountFitViewTextView extends CustomFitViewTextView {
    private boolean inputAdd = false;
    private boolean inputAddShow = false;
    private String inputTemp = "0";

    public InputAmountFitViewTextView(Context context) {
        this(context, null);
    }

    public InputAmountFitViewTextView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public InputAmountFitViewTextView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @Override
    public boolean dispatchKeyEvent(KeyEvent event) {
        if (event.getKeyCode() == KeyEvent.KEYCODE_NUMPAD_0) {
            setAmount("0");
            return false;
        } else if (event.getKeyCode() == KeyEvent.KEYCODE_NUMPAD_1) {
            setAmount("1");
            return false;
        } else if (event.getKeyCode() == KeyEvent.KEYCODE_NUMPAD_2) {
            setAmount("2");
            return false;
        } else if (event.getKeyCode() == KeyEvent.KEYCODE_NUMPAD_3) {
            setAmount("3");
            return false;
        } else if (event.getKeyCode() == KeyEvent.KEYCODE_NUMPAD_4) {
            setAmount("4");
            return false;
        } else if (event.getKeyCode() == KeyEvent.KEYCODE_NUMPAD_5) {
            setAmount("5");
            return false;
        } else if (event.getKeyCode() == KeyEvent.KEYCODE_NUMPAD_6) {
            setAmount("6");
            return false;
        } else if (event.getKeyCode() == KeyEvent.KEYCODE_NUMPAD_7) {
            setAmount("7");
            return false;
        } else if (event.getKeyCode() == KeyEvent.KEYCODE_NUMPAD_8) {
            setAmount("8");
            return false;
        } else if (event.getKeyCode() == KeyEvent.KEYCODE_NUMPAD_9) {
            setAmount("9");
            return false;
        } else if (event.getKeyCode() == KeyEvent.KEYCODE_NUMPAD_DOT) {
            //小数点
            if (getText().length() == 0 || inputAddShow) {
                inputAddShow = false;
                setText("");
                setAmount("0.");
            } else if (!getText().toString().contains(".")) {
                double amount = Double.parseDouble(getText().toString());
                if (amount == 0) {
                    setText("0.");
                } else setAmount(".");
            }
            return false;
        } else if (event.getKeyCode() == KeyEvent.KEYCODE_DEL) {
            //删除
            if (getText().length() > 0) {
                setText(getText().toString().substring(0, getText().length() - 1));
            }
            return false;
        } else if (event.getKeyCode() == KeyEvent.KEYCODE_NUMPAD_ADD) {
//            if (inputAddShow) {
//                if (getText().length() > 0) {
//                    inputAdd = false;
//                    inputTemp = new BigDecimal(inputTemp).add(new BigDecimal(getText().toString())).stripTrailingZeros().toPlainString();
//                    setText(inputTemp);
//                }
//            } else {
//                inputAdd = true;
//                inputAddShow = true;
//                if (getText().length() > 0) {
//                    inputTemp = new BigDecimal(inputTemp).add(new BigDecimal(getText().toString())).stripTrailingZeros().toPlainString();
//                }
//                setText(inputTemp);
//            }


            if (inputAdd && inputAddShow) {

//            inputTemp = new BigDecimal(inputTemp).add(new BigDecimal(getText().toString())).stripTrailingZeros().toPlainString();
//            setText(inputTemp);

//            if (getText().length() > 0) {
//                inputAdd = false;
//                inputTemp = new BigDecimal(inputTemp).add(new BigDecimal(getText().toString())).stripTrailingZeros().toPlainString();
//                setText(inputTemp);
//            }
            } else {

                if (getText().length() > 0 && !inputAddShow) {
                    inputTemp = new BigDecimal(inputTemp).add(new BigDecimal(getText().toString())).stripTrailingZeros().toPlainString();
                    inputAdd = false;
                }
                inputAdd = true;
                inputAddShow = true;
                setText(inputTemp);
            }
            return false;
        }
        return super.dispatchKeyEvent(event);
    }

    private void setAmount(String newNum) {
        if (inputAddShow) {
            inputAddShow = false;
            setText("");
        }
        String value = getText().toString();
        if (TextUtils.isEmpty(value)) {
            setText(newNum);
        } else {
            if (value.contains(".")) {
                int dotIndex = value.indexOf(".");
                //限制小数点后2位
                LogUtil.d("dotIndex", (value.length() - dotIndex) + "");
                if (value.length() - dotIndex > 2)
                    return;
            }
            String joint;
            if (Double.parseDouble(value) == 0) {
                //0.
                if (value.contains(".")) {
                    joint = String.format("%s%s", value, newNum);
                } else {
                    joint = newNum;
                }
            } else {
                joint = String.format("%s%s", value, newNum);
            }

            BigDecimal amount = new BigDecimal(joint);
            if (amount.doubleValue() > BaseConstant.MAX_PRICE)
                return;
            setText(joint);
        }
    }

    public String getSumPrice() {

        if (getText().length() > 0 && !inputAddShow) {
            inputTemp = new BigDecimal(inputTemp).add(new BigDecimal(getText().toString())).stripTrailingZeros().toPlainString();
            setText(inputTemp);
        }
        inputAdd = false;
        inputAddShow = false;
        inputTemp = "0";
        return getText().toString();
    }

    public void clear() {
        inputAdd = false;
        inputAddShow = false;
        inputTemp = "0";
        setText("");
    }
}